var express = require('express');
var router = express.Router();

const order = require("../sql/Order");

const getMonthDate = require("./untils/getMonthDate");

router.get('/', function(req, res, next) {
  const { employeesName } = req.session.userData;

  order.find().then(oData=>{

    const date = getMonthDate();

    const s = new Date(date[0]);
    const e = new Date(date[1]);
    const monthData = oData.filter(val=>{
      const n = new Date(val.OrderDate).getTime();
      return n >= s.getTime() && n <= e.getTime();
    })

    const dayData = oData.filter(val=>{
      const n = new Date(new Date(val.OrderDate).toLocaleDateString()).getTime()
      return n === new Date(new Date().toLocaleDateString()).getTime();
    })

    res.render('index', {
      employeesName,
      index:0,
      monthCount: monthData.length,
      monthMoney: monthData.reduce((prev,val)=>prev+val.LocalCurrencyAmountAfterDiscount, 0).toFixed(2)-0,
      dayCount: dayData.length,
      dayMoney: dayData.reduce((prev,val)=>prev+val.LocalCurrencyAmountAfterDiscount, 0).toFixed(2)-0,
    });

  })
  
});

module.exports = router;
